# coding: utf-8
import urllib2
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.webelement import WebElement
import time
from Tkinter import *
      
class Route:
    def __init__(self, link, title):
	self.link = link
	self.title = title

if __name__=='__main__':
    browser = webdriver.Firefox()
    routeList = []
    routeList.append(
        Route("http://flight.qunar.com/site/oneway_list.htm?searchDepartureAirport=%E6%88%90%E9%83%BD&searchArrivalAirport=\
%E4%B8%8A%E6%B5%B7&searchDepartureTime=2011-10-07&searchArrivalTime=2011-10-10&nextNDays=0&startSearch=true&from=qunarindex", u'成都-上海')
    )
    routeList.append(
        Route("http://flight.qunar.com/site/oneway_list.htm?searchDepartureAirport=%E4%B8%8A%E6%B5%B7&searchArrivalAirport=\
%E8%A5%BF%E5%AE%81&searchDepartureTime=2011-09-16&arrivalTime=2011-09-12&nextNDays=0&searchType=OneWayFlight&startSearch=true&from=sr_trendflash&searchArrivalTime=2011-09-16", u'上海-西宁')
    )
    routn_len = len(routeList)
    i=0
    while True:
	j = i % routn_len
	rount = routeList[j]
        browser.get(rount.link)
        time.sleep(60)
        datebar = browser.find_elements_by_class_name('cur')
        if len(datebar) == 1:
            datebar1 = datebar[0]
            
            curDate = datebar1.find_elements_by_class_name('pr')    
            curDate1 = curDate[0]
            curPrice = curDate1.text[1:]
	    curTime = time.strftime("%H:%M:%S %b,%d %Y", time.localtime())
	    print curTime + ' ' + curPrice + ' ' + rount.title
            if int(curPrice) < 600:     
                
                root.title('Air Price Killing')
                root.geometry('300x200+100+100')
                w1 = Label(root, text= price + '\n' + curTime) 
                w1.pack()
                w2 = Label(root, text= 'Ivan Wang' + '\n' + '0.1Beta') 
                w2.pack(side=RIGHT)
                root.mainloop()
                browser.quit()
                
                break
            #print browser.title
        else:
            print ("Open Error!")
        time.sleep(60)
	i=i+1
   
